// pages/index/search/index.js
const app = getApp();

import {
  homelist
} from '../../../utils/http';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    v: app.globalData.v, //版本
    lang: app.vSwitch(), //版本
    history: [],

    // 分页
    page: 1,
    size: 10,
    pages: 0,
    current: 0,
    noData: false,
    d: [],
    keyWord: '',
    manageRange: '',

    flag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  async init() {
    let t = this;
    if (t.data.keyWord) {
      this.setData({
        page: 1,
        noData: false,
        pages: 0,
        current: 0,
      })
      await t.getMore();
      t.setData({
        flag: true
      })
      let hS = t.data.history ? t.data.history : [];
      hS.forEach((i, k) => {
        if (i == t.data.keyWord) {
          hS.splice(k, 1);
        }
      })
      hS.unshift(t.data.keyWord);
      t.setData({
        history: hS
      })
      wx.setStorageSync('historySearch1', hS)
    } else {
      wx.showToast({
        title: '请输入关键字',
        icon: 'none'
      })
    }
  },


  getMore() {
    let t = this;
    var promise = new Promise((resolve, reject) => {
      if (t.data.pages >= t.data.current) {
        homelist({
          page: t.data.page,
          pageSize: t.data.size,
          keyWord: t.data.keyWord,
          manageRange: t.data.activeId ? t.data.activeId : t.data.manageRange
        }).then(r => {
          if (r.httpCode == 200 || r.code == 0) {
            let d;
            if (t.data.page == 1) {
              d = [];
            } else {
              d = t.data.d;
            }
            if (r.data.records.length) {
              r.data.records.forEach(i => {
                d.push(i)
              })
              if (r.data.records.length < r.data.size) {
                t.setData({
                  noData: true,
                })
              } else {
                t.setData({
                  noData: false,
                })
              }
            } else {
              t.setData({
                noData: true,
              })
              if (t.data.page == 1) {
                wx.showToast({
                  title: '未找到搜索内容',
                  icon: 'none'
                })
              }
            }
            t.setData({
              d,
              page: ++t.data.page,
              pages: r.data.pages,
              current: r.data.current
            })
          }

          resolve();
        })
      } else {
        t.setData({
          noData: true
        })
        resolve();
      }
    });
    return promise;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      history: wx.getStorageSync('historySearch1')
    })
  },
  clear() {
    this.setData({
      keyWord: ''
    })
  },

  tagClick(e) {
    this.setData({
      keyWord: e.mark.text
    })
    this.init();
  },

  clearHistory() {
    this.setData({
      history: []
    })
    wx.removeStorageSync('historySearch1');
  },
  comeHere(e) {
    app.location(e.mark)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noData) {
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.share();
  }
})